﻿namespace Sitecore.ChangesTracker.Archiving
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Web.UI.WebControls;
  using Sitecore.Data.DataProviders.Sql;
  using Sitecore.Collections;
  using Sitecore.Text;
    using Sitecore.Diagnostics;
    using Sitecore.Data.Archiving;

  public class ArchivedItem
  {
    private Guid itemID;
    private string name;
    private Guid templateId;
    private Guid masterId;
    private Guid parentId;
    private DateTime created;
    private DateTime updated;
    private Guid archivalId;
    private List<ArchivedField> fields;
    private ListString languages;
   

    public static List<ArchivedItem> GetItems(Guid archivalId, SqlDataApi api, bool loadFields=false)
    {
      
      SqlStatement st=new SqlStatement();
      st.Select = "select {0}Name{1}, {0}TemplateId{1}, {0}masterId{1}, {0}parentId{1}, {0}created{1}, {0}updated{1}, {0}itemId{1}";
      st.Where = "where {0}archivalid{1}={2}archivalid{3}";
      st.From = "from {0}archiveditems{1}";
      st.AddParameter("archivalid", archivalId);
      List<ArchivedItem> items=new List<ArchivedItem>();

      using (DataProviderReader reader=api.CreateReader(st.Sql,st.GetParameters()))
      {
        while (reader.Read())
        {
          ArchivedItem i = new ArchivedItem();
          i.name = api.GetString(0, reader);
         
          i.templateId = api.GetGuid(1, reader);
          i.masterId = api.GetGuid(2, reader);
          i.parentId = api.GetGuid(3, reader);
          i.created = api.GetDateTime(4, reader);
          i.updated = api.GetDateTime(5, reader);
          i.itemID = api.GetGuid(6,reader);
          i.archivalId = archivalId;
          i.fields=new List<ArchivedField>();
          i.languages = new ListString();
          items.Add(i);
        }
      }
      if (loadFields)
      {
        foreach (ArchivedItem archivedItem in items)
        {
          archivedItem.LoadFields(api);
          foreach (ArchivedField f in archivedItem.Fields)
          {
            if (!string.IsNullOrEmpty(f.Language)&&!archivedItem.languages.Contains(f.Language) )
            {               
              archivedItem.languages.Add(f.Language);
            }
          }
        }
        
      }
      return items;
    }


    void LoadFields(SqlDataApi api)
    {
      SqlStatement st=new SqlStatement();
      st.Select = "select  {0}sharingType{1}, {0}language{1}, {0}version{1}, {0}fieldId{1}, {0}value{1}, {0}created{1}, {0}updated{1}";
      st.From = "from {0}archivedfields{1}";
      st.Where = "where {0}archivalId{1}={2}archivalId{3} and {0}itemId{1}={2}itemId{3}";
      st.AddParameters(new object[] { "archivalId", this.archivalId, "itemId",this.ItemId });
      using (DataProviderReader reader=api.CreateReader(st.Sql,st.GetParameters()))
      {
        while (reader.Read())
        {
          string sharingtype = api.GetString(0, reader);
          string language = api.GetString(1, reader);
          int version = api.GetInt(2, reader);
          Guid fieldid = api.GetGuid(3, reader);
          string value = api.GetString(4, reader);
          DateTime createdf = api.GetDateTime(5, reader);
          DateTime updatedf = api.GetDateTime(6, reader);
          this.Fields.Add(new ArchivedField(this.archivalId,sharingtype,this.ItemId,language,version,fieldid,value,createdf,updatedf));
        }
      }
    }
    public ListString Languages
    {
      get
      {
        return languages;
      }
    }
    public Guid ItemId
    {
      get
      {
        return itemID;
      }
    }

    public string Name
    {
      get
      {
        return this.name;
      }
    }

    public Guid TemplateId
    {
      get
      {
        return this.templateId;
      }
    }

    public Guid MasterId
    {
      get
      {
        return this.masterId;
      }
    }

    public Guid ParentId
    {
      get
      {
        return this.parentId;
      }
    }

    public DateTime Created
    {
      get
      {
        return this.created;
      }
    }

    public DateTime Updated
    {
      get
      {
        return this.updated;
      }
    }

    public Guid ArchivalId
    {
      get
      {
        return this.archivalId;
      }
    }

    public List<ArchivedField> Fields
    {
      get
      {
        return this.fields;
      }
    }
   
  }
}
